// socket

var name = prompt("请输入你的昵称：");
//当发送按钮被点击时
$('form').submit(function () {
    var msg = $("#m").val() //获取用户输入的信息
    socket.emit("message", msg) //将消息发送给服务器
    $("#m").val("") //置空消息框
    return false //阻止form提交
})

function addLine(msg) {
    $('#messages').append($('<li>').text(msg));
}


// socket 初始化
var socket = io.connect('http://10.9.111.192:3000/some');
socket.on('connect', function () {
    //发送昵称给后端，并更改网页title
    socket.emit("join", name)
    document.title = name + "的群聊"
})
socket.on("join", function (user) {
    addLine(user + " 加入了群聊")
})

//接收到服务器发来的message事件
socket.on("message", function (msg) {
    addLine(msg)
    sendDanmaku(msg)
})
// x5-video-player-type="h5"

// dplayer 初始化 DPlayer

const dp = new DPlayer({
    container: document.getElementById('dplayer'),
    danmaku: {
        id: '9E2E3368B56CDBB4',
        api: '/dplayer/', // dplayer发送弹幕请求命令空间  连接会成为【命名空间】/v3
        token: 'tokendemo',
        maximum: 1000,
        user: 'DIYgod',
        bottom: '15%',
        unlimited: true
    },
    video: {
        url: 'https://api.dogecloud.com/player/get.mp4?vcode=5ac682e6f8231991&userId=17&ext=.mp4'
    },
});
document.getElementsByTagName("video")[0].setAttribute("x5-video-player-type", "h5").setAttribute("x5-video-player-fullscreen", "false");

// dplayer 发送弹幕
function sendDanmaku(msg) {
    dp.danmaku.send({
        text: msg,
        color: '#FFFFFF',
        type: 'right'   // should be `top` `bottom` or `right`
    }, function () {
        console.log('success');
    });
}
